package com.qdxwx.web.controller.book;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.book.BookQuery;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Course接口
 * @author developer
 */
@Controller
@RequestMapping("/book")
public class BookIndexController extends BaseController {

    @Autowired
    private ChannelService channelService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BookService bookService;
    @Autowired
    private TeacherService teacherService;

    @RequestMapping({"/index.html", "/list.html"})
    public String index(HttpServletRequest request, ModelMap model) {
        if (TKRequest.isMobileBrowse(request)) {
            return getViewPathH5("book/book-list");
        }
        int channelId = 9;//教材模块
        Channel channel = channelService.get(channelId);
        if (channel == null) {
            return toError404(request);
        }
        //禁用的频道
        if (channel.getIsDisabled() == 1) {
            return toError404(request);
        }
        //判断是否属于模块
        if (channel.getModuleId() != ModuleType.BOOK.ordinal()) {
            return toError404(request);
        }
        int categoryId = TKConvert.toInteger(request.getParameter("categoryId"));
        int categoryParentId = 0;
        // 查询所有1级分类
        List<Category> categoryRoots = categoryService.getRoots(channelId);
        model.put("categoryRoots", categoryRoots);

        if (categoryId > 0) {
            Category category = categoryService.get(categoryId);
            if (category == null) {
                categoryId = 0;
            } else {
                categoryParentId = category.getParentId();
                model.put("category", category);
            }
            List<Category> categoryChildren;
            //父Id大于0,获得它的同级专业
            if (categoryParentId > 0) {
                categoryChildren = categoryService.getChildren(categoryParentId);
                Category parent = categoryService.get(categoryParentId);
                model.put("categoryParent", parent);//父级 专业名
            } else {
                //获得它的子级专业
                categoryChildren = categoryService.getChildren(categoryId);
            }
            model.put("categoryChildren", categoryChildren);
        }
        model.put("categoryParentId", categoryParentId);//父级id
        BookQuery query = new BookQuery();
        query.setIsAvailable(1);
        query.setIsElite(1);
        List<Book> bookEliteList = bookService.getList(query, new PageRequest(1, 6));
        model.put("bookEliteList", bookEliteList);
        query.setCategoryId(categoryId);
        query.setOrderBy(TKConvert.toInteger(request.getParameter("orderBy")));
        query.setIsHot(TKConvert.toInteger(request.getParameter("isHot")));
        query.setIsElite(TKConvert.toInteger(request.getParameter("isElite")));
        query.setIsFree(TKConvert.toInteger(request.getParameter("isFree")));
        PageRequest page = new PageRequest();
        page.setCurrentPage(TKConvert.toInteger(request.getParameter("page")));
        page.setPageSize(10);
        List<Book> bookList = bookService.getList(query, page);
        if (bookList != null) {
            for (Book book : bookList) {
                book.setContext(teacherService.getNamesByBookId(book.getId()));
            }
        }
        int total = bookService.count(query);
        model.put("query", query);
        model.put("bookList", bookList);
        model.put("totalPages", page.getTotalPages(total));
        model.put("page", page);
        model.put("channel", channel);
        model.put("userId", LoginUser.getUserId(request));
        return getViewPath("book/book-list");
    }
}
